/*
 * bugs
 * ie resize from left to very right! widget jump
 * all, resize from corner fire start/complete events twice
 */
if (inweo.get("inweo.widgets.resizer") != true) {
	inweo.set("inweo.widgets.resizer", true);
	inweo.widgets.resizer = inweo.core.widget.extend({
		__start: [],
		__end: [],
		__resize: [],
		handlers: null,
		initialize: function(object) {
			if ($type(object) == "element") {
				this.object = object;
			} else {
				this.object = object.widget;
			}

			this.widget = new Element("div");
			this.widget.addClass("resizer");
			this.registerWidget("resizer");

			this.object.addClass("resizeObject");
			this.object.injectInside(this.widget);

			// set defaults
			this.widget.setStyle("min-width", this.object.getStyle("min-width") || "100px");
			this.widget.setStyle("max-width", this.object.getStyle("max-width"));
			this.widget.setStyle("min-height", this.object.getStyle("min-height") || "100px");
			this.widget.setStyle("max-height", this.object.getStyle("max-height"));

			this.handlers = {
								"nwResizer"	: null,
								"wResizer"	: null,
								"swResizer" : null,
								"sResizer"	: null,
								"seResizer" : null,
								"eResizer"	: null,
								"neResizer"	: null,
								"nResizer"	: null
							};

			for (var name in this.handlers) {
				this.handlers[name] = new Element("div");
				this.handlers[name].addClass(name);
				this.handlers[name].setProperty("resizer", name);
				this.handlers[name].addClass("resizerHandler");
				this.handlers[name].injectInside(this.widget);
				new inweo.core.event(this.handlers[name]).addEvent("mousedown", function(event) {
					document.body.addClass(event.target.getProperty("resizer") + "Body");
				});
			}
			var self = this;
			new inweo.core.event($$("body")[0]).addEvent("mouseup", function(event) {
				for (var name in self.handlers) {
					document.body.removeClass(name + "Body");
				}
			});
			//this.render(); /* I hate you ie */
		},
		__onResize: function() {
			this.object.setStyle("width", this.widget.offsetWidth - 2);
			this.object.setStyle("height", this.widget.offsetHeight - 2);
			inweo.execute(this.__resize, this);
		},
		onResize: function() {
			this.__resize.push(fn);
		},
		__onStart: function() {
			this.object.addClass("resizing");
			this.widget.addClass("resizing");
			inweo.execute(this.__start, this);
		},
		onStart: function(fn) {
			this.__start.push(fn);
		},
		__onEnd: function() {
			this.object.removeClass("resizing");
			this.widget.removeClass("resizing");
			inweo.execute(this.__end, this);
		},
		onEnd: function(fn) {
			this.__end.push(fn);
		},
		render: function() {
			// this code is screaming "get me into loop" NOT
			var self = this;
			var left, top, width, height, right, bottom = null;

			//static? it can be changed
			var minWidth = this.object.getStyle("min-width").toInt();
			var maxWidth = this.object.getStyle("max-width").toInt();
			var minHeight = this.object.getStyle("min-height").toInt();
			var maxHeight = this.object.getStyle("max-height").toInt();

			new Drag.Move(self.widget, {
				limit: {x: [minWidth, maxWidth], y: [minHeight, maxHeight]},
				modifiers: {
					"x": null,
					"y": "height"
				},
				onStart: function() {
					self.__onStart();
				},
				onDrag: function() {
					self.__onResize();
				},
				onComplete: function() {
					self.__onEnd();
				},
				handle: [this.handlers.sResizer, this.handlers.swResizer, this.handlers.seResizer]
			});
			new Drag.Move(self.widget, {
				limit: {x: [minWidth, maxWidth], y: [minHeight, maxHeight]},
				modifiers: {
					"x": "width",
					"y": null
				},
				onStart: function() {
					self.__onStart();
				},
				onDrag: function() {
					self.__onResize();
				},
				onComplete: function() {
					self.__onEnd();
				},
				handle: [this.handlers.eResizer, this.handlers.seResizer, this.handlers.neResizer]
			});
			new Drag.Move(self.widget, {
				modifiers: {
					"x": "left",
					"y": null
				},
				onBeforeStart: function(){
					var coordinates = self.widget.getCoordinates();
					left = self.widget.offsetLeft;
					width = self.widget.offsetWidth;
					maxLeft = left + (width - minWidth);
					right = coordinates.right;
				},
				onStart: function(){
					self.__onStart();
				},
				onDrag: function(){
					self.widget.setStyle("width", width - (self.widget.offsetLeft - left));
					if ((self.widget.getStyle("width").toInt() + 3) < self.widget.offsetWidth) {
						/*if (document.body.id == "ie") {
							//if ((self.widget.getStyle("left").toInt() + 10) > (maxLeft + self.widget.offsetWidth.toInt())) {
								this.stop();
								self.__onEnd();
							//}
						}*/
						self.widget.setStyle("left", maxLeft);
					} else {
						self.widget.style.position = "relative";
					}
					if ((self.widget.getStyle("width").toInt() + 3) > maxWidth) {
						self.widget.setStyle("left", right - maxWidth);
					}
					self.__onResize();
				},
				onComplete: function(){
					self.__onEnd();
				},
				handle: [this.handlers.wResizer, this.handlers.swResizer, this.handlers.nwResizer]
			});
			new Drag.Move(self.widget, {
				modifiers: {
					"x": null,
					"y": "top"
				},
				onBeforeStart: function() {
					var coordinates = self.widget.getCoordinates();
					top = self.widget.offsetTop;
					height = self.widget.offsetHeight;
					maxTop = top + (height - minHeight);
					bottom = coordinates.bottom;
					//ie
					self.widget.setStyle("width", self.widget.offsetWidth);
				},
				onStart: function() {
					self.__onStart();
				},
				onDrag: function() {
					self.widget.setStyle("height", height - (self.widget.offsetTop - top));
					if ((self.widget.getStyle("height").toInt() + 3) < self.widget.offsetHeight) {
						self.widget.setStyle("top", maxTop);
					}
					if ((self.widget.getStyle("height").toInt() + 3) > maxHeight) {
						self.widget.setStyle("top", bottom - maxHeight);
					}
					self.__onResize();
				},
				onComplete: function() {
					self.__onEnd();
				},
				handle: [this.handlers.nResizer, this.handlers.nwResizer, this.handlers.neResizer]
			});
		},
		disable: function(bool) {
			if (bool == true) {
				this.widget.addClass("resizerHandlerDisable");
			} else if (bool == false) {
				this.widget.removeClass("resizerHandlerDisable");				
			}
		}
	});
}